﻿using IceZero.Business.Models;
using IceZero.DataStore;
using IceZero.DataStore.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IceZero.Business
{
    /// <summary>
    /// 
    /// </summary>
    public class ShortLinkBusiness
    {
        /// <summary>
        /// 
        /// </summary>
        public ShortLinkDataStore ShortLinkDataStore { get; }
        public ShortLinkBusiness(ShortLinkDataStore shortLinkDataStore)
        {
            ShortLinkDataStore = shortLinkDataStore;
        }

        /// <summary>
        /// 创建短链接
        /// </summary>
        /// <param name="rawUrl">原始链接</param>
        /// <param name="effectSeconds">有效秒数，默认600秒(60-604800[7天])</param>
        /// <returns></returns>
        public ShortLink CreateShortLink(string rawUrl, int effectSeconds = 600)
        {
            if (effectSeconds < 60)
            {
                throw new IceZeroBusinessException("the min value is 60");
            }
            if (effectSeconds > 604800)
            {
                throw new IceZeroBusinessException("the max value is 604800");
            }
            var now = DateTime.Now;
            var shortLink = new ShortLink()
            {
                Code = "C" + Guid.NewGuid().ToString("N"),
                RawUrl = rawUrl,
                CreateTime = now,
                ExpireTime = now.AddSeconds(effectSeconds)
            };
            var id = ShortLinkDataStore.Save<string>(shortLink);
            shortLink.Id = id;
            return shortLink;
        }

        /// <summary>
        /// 通过 id 获取短连接
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ShortLink GetShortLinkInfoById(string id)
        {
            var link = ShortLinkDataStore.QueryById(id);
            if (link == null)
            {
                throw new IceZeroBusinessException("not match this id data");
            }
            if (link.ExpireTime < DateTime.Now)
            {
                throw new IceZeroBusinessException("link has expired");
            }
            return link;
        }

        /// <summary>
        /// 通过 code 获取短连接
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public ShortLink GetShortLinkInfoByCode(string code)
        {
            var link = ShortLinkDataStore.QueryOne("Code", code);
            if (link == null)
            {
                throw new IceZeroBusinessException("not match this id data");
            }
            if (link.ExpireTime < DateTime.Now)
            {
                throw new IceZeroBusinessException("link has expired");
            }
            return link;
        }

        /// <summary>
        /// 获取所有链接
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public List<ShortLink> GetAllShortLinks()
        {
            var links = ShortLinkDataStore.QueryAll();
            return links;
        }
    }
}
